import numpy as np

def sigmoid(x,deriv = False):
    if(deriv):
        return x*(1-x)
        # return np.exp(-x)/np.sqrt(1+np.exp(-x))
    return 1/(1+np.exp(-x))
#输入
x = np.array(
    [
        [0.98,0.99,0.94],
        [0.91,0.01,0.97],
        [0.01,0.87,0.89],
        [0.06,0.08,0.01],
        [0.03,0.02,0.95],
        [0.89,0.02,0.05],
    ]
)
#正确值
y = np.array([
    [1],
    [1],
    [1],
    [0],
    [0],
    [0]
])
#固定随机种子，每次随机都一样
# np.random.seed(0)

# 初始化w0,w1权重
w0 = 2*np.random.random((3,4)) - 1

w1 = 2*np.random.random((4,1)) -1

for i in range(10000):
    # 隐层1
    l0 = sigmoid(np.matmul(x,w0))
    #输出值
    l1 = sigmoid(np.matmul(l0,w1))
    # print('l1:',l1)

    if(i%3000==0):
        loss = np.mean(np.abs(y-l1))
        print('loss:',loss)
    # 正确值和预期值之间的差异值
    l1_error = y - l1
    # print("l1_error",l1_error)

    l1_delta = l1_error * sigmoid(l1,True)
    # print('l1_delta:',l1_delta)

    l0_error = np.matmul(l1_delta,w1.T)
    l0_delta = l0_error * sigmoid(l0,True)

    w1+= l0.T.dot(l1_delta)
    w0+= x.T.dot(l0_delta)


# np.save("d:/box/demo20.npy",(w0,w1))

print(w1)
print(w0)

print("save success")